import 'dart:io';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/ui/my/withdrawal/withdrawal_viewmodel.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/xfg_text.dart';

/// @description:
/// @author tangzhentao
/// @created at 10:38 2024/12/15

class WithdrawalPage extends StatelessWidget {
  const WithdrawalPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ProviderScaffold<WithdrawalViewmodel>(
      model: WithdrawalViewmodel(),
      appBar: (_) => xfgAppbar(title: '提现'),
      builder: (ctx, withdrawal) => SingleChildScrollView(
        padding: EdgeInsets.fromLTRB(XfgSize().size10, XfgSize().size12, XfgSize().size10, 0),
        child: Column(
          children: [
            Ink(
              height: XfgSize().size95,
              decoration: white6Decoration,
              child: InkWell(
                onTap: withdrawal.bindWechat,
                borderRadius: BorderRadius.circular(XfgSize().size6),
                child: Builder(builder: (ctx) {
                  final wechat = ctx.select<WithdrawalViewmodel, String?>((k) => k.wechat);
                  return Padding(
                    padding: EdgeInsets.only(left: XfgSize().size20, right: XfgSize().size16),
                    child: Row(
                      children: [
                        Image.asset(Res.imagesWithdrawalWechat, width: XfgSize().size28, height: XfgSize().size28),
                        SizedBox(width: XfgSize().size10),
                        Expanded(child: XfgText(wechat?? '添加微信提现收款账号', fontSize: XfgSize().text18, color: wechat?.isNotEmpty == true ? XfgColor.text1:XfgColor.text3)),
                        Visibility(visible: wechat?.isNotEmpty == true, child: XfgText('切换 ', fontSize: XfgSize().text12, color: XfgColor.colorB5)),
                        Image.asset(wechat?.isNotEmpty == true ? Res.imagesWithdrawalWechatReplace:Res.imagesWithdrawalWechatAdd, width: XfgSize().size16, height: XfgSize().size16),
                      ],
                    ),
                  );
                }),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: XfgSize().size10, bottom: XfgSize().size44),
              decoration: white6Decoration,
              child: Column(
                children: [
                  Container(
                    padding: EdgeInsets.fromLTRB(XfgSize().size14, XfgSize().size16, XfgSize().size14, XfgSize().size22),
                    alignment: Alignment.centerLeft,
                    child: XfgText('提现金额', fontSize: XfgSize().text16, color: XfgColor.text1),
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: XfgSize().size14),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        XfgText('¥ ', fontSize: XfgSize().text16, color: XfgColor.text1D),
                        Expanded(child: TextField(
                          controller: withdrawal.moneyController,
                          focusNode: withdrawal.moneyNode,
                          maxLines: 1,
                          keyboardType: const TextInputType.numberWithOptions(decimal: true),
                          style: TextStyle(color: XfgColor.text1D, fontSize: XfgSize().size24, height: Platform.isIOS ? 1:null),
                          onChanged: withdrawal.onChanged,
                          decoration: InputDecoration(
                            isDense: true,
                            border: InputBorder.none,
                            contentPadding: EdgeInsets.zero,
                            hintText: '最低提现金额1元',
                            hintStyle: TextStyle(color: XfgColor.colorB5, fontSize: XfgSize().size24, height: Platform.isIOS ? 1:null),
                          ),
                        ))
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: XfgSize().size20),
                    padding: EdgeInsets.fromLTRB(XfgSize().size12, 0, XfgSize().size16, 0),
                    height: XfgSize().size42 + XfgSize().size0_5,
                    decoration: BoxDecoration(border: Border(top: BorderSide(color: XfgColor.colorE, width: XfgSize().size0_5))),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        XfgText('当前余额：', fontSize: XfgSize().text16, color: XfgColor.black, children: [
                          TextSpan(text: ctx.select<WithdrawalViewmodel, double>((k) => k.balance).toStringAsFixed(2), style: const TextStyle(color: XfgColor.theme, fontWeight: FontWeight.bold))
                        ]),
                        GestureDetector(
                          onTap: withdrawal.withdrawalDetails,
                          child: XfgText('提现记录', fontSize: XfgSize().text16, color: XfgColor.black),
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
            TextButton(
                onPressed: ctx.select<WithdrawalViewmodel, bool>((k) => k.canClick) ? withdrawal.withdrawalWechat:null,
                style: xfgButtonStyle(Size(XfgSize().size355, XfgSize().size44), radius: XfgSize().size44),
                child: XfgText('提现到微信余额', fontSize: XfgSize().text16, color: XfgColor.white, bold: FontWeight.bold)
            ),
            Container(
              margin: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size40, XfgSize().size12, 0),
              padding: EdgeInsets.fromLTRB(XfgSize().size12, XfgSize().size8, XfgSize().size16, XfgSize().size18),
              alignment: Alignment.centerLeft,
              decoration: BoxDecoration(border: Border.all(color: XfgColor.colorB5, width: XfgSize().size0_5), borderRadius: BorderRadius.circular(XfgSize().size6)),
              child: XfgText('提现规则：', fontSize: XfgSize().text16, color: XfgColor.colorB5, bold: FontWeight.bold, height: 1.6, children: const [
                TextSpan(text: '\n1:只能提现钱包余额'
                    '\n2:单笔最小提现金额元，单笔最大提现金额元'
                    '\n3:提现申请提交后，我们会在两小时内完成审核，审核失败提现金额会退回到余额'
                    '\n4:提现审核成功后，您的提现将会在24小时内到账', style: TextStyle(fontWeight: FontWeight.normal))
              ]),
            )
          ],
        ),
      ),
    );
  }

}
